package com.example.xxpk.config.handler;


import cn.dev33.satoken.exception.NotLoginException;
import com.example.xxpk.common.ex.UserValidatorException;
import com.example.xxpk.common.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Throwable.class)
    public R handleThrowable(Throwable e, HttpServletRequest request) {
        e.printStackTrace();        // 1: 一定要加下面这行代码。打印异常堆栈信息，方便程序员去根据异常排查错误 --服务开发者

        return R.error();
    }

    @ExceptionHandler(RuntimeException.class)
    public R handleRuntimeException(RuntimeException e, HttpServletRequest request) {
        // 1: 一定要加下面这行代码。打印异常堆栈信息，方便程序员去根据异常排查错误 --服务开发者
        e.printStackTrace();
        return R.error(e.getMessage());
    }

    @ExceptionHandler(UserValidatorException.class)
    public R handleUserValidatorException(UserValidatorException e, HttpServletRequest request) {
        // 1: 一定要加下面这行代码。打印异常堆栈信息，方便程序员去根据异常排查错误 --服务开发者
        e.printStackTrace();
        return R.error(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    public R handleNotLoginException(NotLoginException e, HttpServletRequest request) {
        e.printStackTrace();
        return R.error(e.getCode(),e.getMessage());
    }

}
